package com.example.cardgame
import android.Manifest
import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.widget.Button
import android.widget.ImageButton
import android.widget.ImageView
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity

class MainActivity : AppCompatActivity() {
    private lateinit var userInfo: MutableMap<String, String?>
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        // 初始化显示的玩家信息
        initializeUserInfo()

        // 设置按钮点击事件
        val buttonModifyInfo = findViewById<Button>(R.id.buttonModifyInfo)
        buttonModifyInfo.setOnClickListener {
            val intent = Intent(this@MainActivity, ModifyInfoActivity::class.java)
            intent.putExtras(createIntentBundle(userInfo))
            startActivityForResult(intent, 1)
        }

        val buttonCardGame = findViewById<Button>(R.id.buttonCardGame)
        buttonCardGame.setOnClickListener {
            val intent = Intent(this@MainActivity, CardGameActivity::class.java)
            startActivityForResult(intent,2)
        }
    }

    // 重写onActivityResult方法来接收从ModifyInfoActivity返回的数据
    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode == RESULT_OK && data != null) {
            if (requestCode == 1) {
                // 从修改信息页面返回的数据
                userInfo = data.extras?.toMutableMap() ?: userInfo
            } else if (requestCode == 2) {
                // 从游戏页面返回的分数
                val newScore = data.getIntExtra("score", 0)
                val currentHighScore = userInfo["highScore"]?.toIntOrNull() ?: 0
                if (newScore > currentHighScore) {
                    // 更新最高分
                    userInfo["highScore"] = newScore.toString()
                }
            }
            updateUserInfo()
        }
    }


    // 提供一个扩展函数将 Bundle 转换为 MutableMap
    fun Bundle.toMutableMap(): MutableMap<String, String?> {
        val map = mutableMapOf<String, String?>()
        for (key in this.keySet()) {
            map[key] = this.getString(key) // 这里假设您只需要字符串
        }
        return map
    }


    // 定义一个方法来更新UI
    private fun updateUserInfo() {
        val textViewName = findViewById<TextView>(R.id.textViewName)
        val textViewGender = findViewById<TextView>(R.id.textViewGender)
        val textViewStudentID = findViewById<TextView>(R.id.textViewStudentID)
        val textViewPhone = findViewById<TextView>(R.id.textViewPhone)
        val textViewEmail = findViewById<TextView>(R.id.textViewEmail)
        val textViewHobbies = findViewById<TextView>(R.id.textViewHobbies)
        val textViewHighScore = findViewById<TextView>(R.id.textViewHighScore)
        val imageViewAvatar = findViewById<ImageView>(R.id.imageViewAvatar)
        val avatarUri = userInfo["avatarUri"]
        if (!avatarUri.isNullOrBlank()) {
            imageViewAvatar.setImageURI(Uri.parse(avatarUri))
        }
        textViewName.text = "姓名：${userInfo["name"]}"
        textViewGender.text = "性别：${userInfo["gender"]}"
        textViewStudentID.text = "学号：${userInfo["studentID"]}"
        textViewPhone.text = "电话：${userInfo["phone"]}"
        textViewEmail.text = "邮件地址：${userInfo["email"]}"
        textViewHobbies.text = "爱好：${userInfo["hobbies"]}"
        textViewHighScore.text = "游戏最高分：${userInfo["highScore"]}"
    }

    // 提供一个扩展函数将 MutableMap 转换为 Intent Bundle
    private fun createIntentBundle(map: Map<String, String?>): Bundle {
        val bundle = Bundle()
        map.forEach { (key, value) ->
            bundle.putString(key, value)
        }
        return bundle
    }

    // 初始化用户信息
    private fun initializeUserInfo() {
        userInfo = mutableMapOf(
            "name" to "张三",
            "gender" to "男",
            "studentID" to "123456",
            "phone" to "1234567890",
            "email" to "zhangsan@example.com",
            "hobbies" to "阅读, 游戏, 购物",
            "highScore" to "0",
            "avatarUri" to null  // 初始化头像 URI
        )
        updateUserInfo()
    }
}
